home *** CD-ROM | disk | FTP | other *** search
- Path: news.lpr.carel.fi!usenet
- From: Ari Lukumies <aril@cmt.lpr.mail.carel.fi>
- Newsgroups: comp.lang.c++
- Subject: Re: Structure member offsets at compile time - help!
- Date: Tue, 06 Feb 1996 18:05:26 +0200
- Organization: Carelcomp Forest
- Message-ID: <31177C46.6554@cmt.lpr.mail.carel.fi>
- References: <4f6qt5$d16@maureen.teleport.com>
- NNTP-Posting-Host: renoir.cclahti.carel.fi
- Mime-Version: 1.0
- Content-Type: text/plain; charset=us-ascii
- Content-Transfer-Encoding: 7bit
- X-Mailer: Mozilla 2.0b6a (WinNT; I)
-
- Jeff Grossman wrote:
- >
- > I'm looking for a way to get an int offset to a structure member
- > at compile time.
- >
- > The reason is that I need to build a table of addresses of structure
- > members (if you must know, its for simplifying the process of doing
- > hundreds of ODBC SQLBindCol calls).
- >
- > Right now I can create a table with addresses that point to a structure
- > on the stack. I cannot use a static structure or a heap structure
- > for these reasons:
- >
- > 1) if the structure is static, it is not thread safe. If I protect
- > it with a critical section, it introduces too much serialization into
- > the application
- >
- > 3) if the structure is on the heap, there is no way to create a compile
- > time table (if I could figure that one out, I wouldn't need to post
- > this question <g>! )
- >
- > The drawback to the existing method is the huge amount of code generated
- > to fill this structure at each procedure entry, If I had a compile
- > time static structure with int offsets, I could add in the actual
- > structure address as I needed.
- >
- > Unfortunately, nothing I've tried works. I even tried a kludges such as:
- >
- > (char)(&Struc.item) - (char)(&Struc)
- >
- > This gives me int offsets. However, the compiler is not smart enough
- > (even with optimization) to figure out that this is a compile time
- > constant and generates code to do the pointer arithmetic.
- >
- > Does anyone know a trick to get compile time structure offsets?
- >
- > TIA.
- >
- > Jeff
-
- Check out the 'offsetof' macro declared in <stddef.h>.
-
- Later,
- AriL
-
- --
- All my opinions are mine and mine alone.
-